<?php
class payResult{
	
	function Pay_log($msg,$lavel)
	{
		error_log(date('Y-m-d H:i:s',time())." ".$lavel." - ".$msg . ";\r\n", 3, DIR_FS_SQL_CACHE."/glbpay-info-".date('Y-m-d',time()).".log");
	}

	//支付返回结果处理
	function UpdateOrder($result){	
		global $messageStack,$db;
		if(empty($result)) 
		{ 
			$this->Pay_log(MODULE_PAYMENT_GLBPAY_API_ERROR_TIMEOUT,'INFO');
			$messageStack->add_session(FILENAME_ACCOUNT, MODULE_PAYMENT_GLBPAY_API_ERROR_TIMEOUT, 'error');			
			zen_redirect(zen_href_link(FILENAME_ACCOUNT, '', 'SSL'));
			
		}else{
			$payRes=json_decode($result);	
			//如果返回的状态不是异常,就进行签名验证
			if((!empty($payRes->status))&&$payRes->status!="9")
			{
				$strcontent=$payRes->version . 'en-us' . $payRes->merchantid . $payRes->orderid . $payRes->orderdate . $payRes->ordercurrency. $payRes->orderamount . $payRes->paycurrency .  $payRes->payamount .$payRes->remark1 . $payRes->remark2 .$payRes->remark3  . $payRes->transid . $payRes->transdate . $payRes->status.$payRes->respcode.$payRes->respmsg.$payRes->redirectform;
				if(!empty($payRes->dccinfo)){//如果是DCC返回,就加上DCC的相关信息
					$strcontent=$strcontent.$payRes->dccinfo->sourceamount.$payRes->dccinfo->sourcecurrency.$payRes->dccinfo->targetamount.$payRes->dccinfo->targetcurrency.$payRes->dccinfo->exchangerate;
				}
				$getsignature=md5(MODULE_PAYMENT_GLBPAY_SSLKEY . $strcontent);
				if($getsignature!=$payRes->signature)//signature
				{
					$this->Pay_log('check sign failed','INFO');
					$messageStack->add_session(FILENAME_ACCOUNT, MODULE_PAYMENT_GLBPAY_API_ERROR_SIGNATURE_ERROR, 'error');
					$this->Pay_log('Feedback signStr:'.MODULE_PAYMENT_GLBPAY_SSLKEY . $strcontent,'INFO');
					zen_redirect(zen_href_link(FILENAME_ACCOUNT, '', 'SSL'));
				}
				$this->Pay_log('check sign success','INFO');
			}
			//签名验证通过,就判断返回的状态
			switch($payRes->status){
				case '1': 
				{
					//支付成功,检查订单状态,如果订单状态是未更新,则更新订单状态
					$this->Pay_log('api result is success','INFO');	
					$new_order_id=$payRes->remark2;
					$_SESSION['order_number_created']=$new_order_id;
					$order_id=$payRes->orderid;  
					$ordersql="select * from " . TABLE_ORDERS . " where orders_id = '" . (int)$new_order_id."'";
					$orderinfo=$db->Execute($ordersql);
					if($orderinfo->recordCount() <1)
					{ 	
						$this->Pay_log('zencart could not find the records with the orderid :'.$new_order_id,'INFO');
						$messageStack->add_session(FILENAME_ACCOUNT, MODULE_PAYMENT_GLBPAY_API_ERROR_BUSINESS_ERROR, 'error');
						zen_redirect(zen_href_link(FILENAME_ACCOUNT, '', 'SSL'));
					}  
					if($orderinfo->fields['orders_status'] == MODULE_PAYMENT_GLBPAY_ORDER_STATUS_ID)
					{
					    $this->Pay_log('zencart found the order has been successed','INFO');		 
						$_SESSION['cart']->reset(true);
						unset($_SESSION['sendto']);
						unset($_SESSION['billto']);
						unset($_SESSION['shipping']);
						unset($_SESSION['payment']);
						unset($_SESSION['comments']);
						zen_redirect(zen_href_link(FILENAME_CHECKOUT_SUCCESS, '', 'SSL'));
					}
					elseif($orderinfo->fields['orders_status']!=MODULE_PAYMENT_GLBPAY_PROCESSING_STATUS_ID)
					{ 	
						$this->Pay_log('order status aleady been changed, orderid:'.$order_id,'INFO');
						$messageStack->add_session('account', vsprintf(MODULE_PAYMENT_GLBPAY_API_ERROR_ORDERSTATUS_ERROR,$new_order_id));
						zen_redirect(zen_href_link(FILENAME_ACCOUNT, '', 'SSL'));
					} 
					$this->Pay_log('S2S change order status failed,redo this action again, orderid:'.$order_id,'INFO');	
					$sql_order_update=array('orders_status' => MODULE_PAYMENT_GLBPAY_ORDER_STATUS_ID);
					zen_db_perform(TABLE_ORDERS, $sql_order_update, 'update', 'orders_id=' . (int) $new_order_id);
					$sql_data_array = array('orders_id' => $new_order_id,
											'orders_status_id' => MODULE_PAYMENT_GLBPAY_ORDER_STATUS_ID,
											'date_added' => 'now()',
											'comments' => 'OrderID:'. $payRes->transid,
											'customer_notified' => '0'
										);
					zen_db_perform(TABLE_ORDERS_STATUS_HISTORY, $sql_data_array);
					$_SESSION['cart']->reset(true);
					unset($_SESSION['sendto']);
					unset($_SESSION['billto']);
					unset($_SESSION['shipping']);
					unset($_SESSION['payment']);
					unset($_SESSION['comments']);
					$this->Pay_log('change the order status success when feedback, orderid:'.$order_id,'INFO');
					zen_redirect(zen_href_link(FILENAME_CHECKOUT_SUCCESS, '', 'SSL')); 
				}
				case '2': 
				{
				    //银行扣款时返回错误,需要显示错误信息
					$messageStack->add_session(FILENAME_ACCOUNT, MODULE_PAYMENT_GLBPAY_API_ERROR_PAYMENT_ERROR.'('.($payRes->respmsg).')', 'error');
					$this->Pay_log('bank response with error:'.$payRes->respmsg,'INFO');
					zen_redirect(zen_href_link(FILENAME_ACCOUNT, '', 'SSL')); 					
					break;
				}
				case '9':
				{
					//统错误放一块儿,只显示错误代码,不显示具体错误信息
					$messageStack->add_session(FILENAME_ACCOUNT, MODULE_PAYMENT_GLBPAY_API_ERROR_BUSINESS_ERROR.'('.($payRes->status).')', 'error');
					$this->Pay_log('payment response with error:'.$payRes->respmsg,'INFO');
					zen_redirect(zen_href_link(FILENAME_ACCOUNT, '', 'SSL')); 					
					break;
				}					
				case '5':
				{
					//3D支付,需要发生跳转
					echo $payRes->redirectform;
					exit();
					break;
				}
				case '6':
				{
					//DCC交易,需要发生跳转
					$actionurl=zen_href_link('api_dccpay.php','','SSL',false,false,true);
					$dccinfo=$payRes->dccinfo;
					$dccStr='<form  name="formdcc" id="formdcc" method="post" action="'.$actionurl.'" >'
							.'<input type="hidden" id="exchangerate" name="exchangerate" value="'.$dccinfo->exchangerate.'" />'
							.'<input type="hidden" id="sourceamount" name="sourceamount" value="'.$dccinfo->sourceamount.'" />'
							.'<input type="hidden" id="sourcecurrency" name="sourcecurrency" value="'.$dccinfo->sourcecurrency.'" />'
							.'<input type="hidden" id="targetamount" name="targetamount" value="'.$dccinfo->targetamount.'" />'
							.'<input type="hidden" id="targetcurrency" name="targetcurrency" value="'.$dccinfo->targetcurrency.'" />'
							.'<input type="hidden" id="txnid" name="txnid" value="'.$payRes->transid.'" />'
							.'</form>'
							.'<script type="text/javascript"> document.formdcc.submit()</script>';	
				     echo $dccStr;
					 exit();
					 break; 
				} 					 
				default:
				{
					//未知状态,先当做异常跳转
					//$_SESSION['cart']->reset(true);
					$messageStack->add_session(FILENAME_ACCOUNT, MODULE_PAYMENT_GLBPAY_API_ERROR_PAYMENT_ERROR.'('.($payRes->respmsg).')', 'error');
					$this->Pay_log('payment response with some errors:'.$payRes->respmsg,'INFO');
					zen_redirect(zen_href_link(FILENAME_ACCOUNT, '', 'SSL'));					
				}
			}
		}
	}
}
?>